<template>
  <van-dialog
    use-slot
    title="提示"
    :show="show"
    show-cancel-button
    confirm-button-open-type="getUserInfo"
    @close="onClose"
    @getuserinfo="wxLogin"
  >
   <div class="body">
     <div class="page__bd page__bd_spacing">

             <div class="icon-box">
                 <i class="weui-icon-info weui-icon_msg"></i>

             </div>

         </div>
     <span style="width: 100%;color: #0BB20C;font-size: 36rpx;margin:20rpx 0">接受授权</span>
     <span style="width: 100%;margin-bottom: 20rpx;font-size: 22rpx">为了更好的为您提供服务,此应用需要您的授权</span>
<!--    <button-->
<!--         open-type="getUserInfo"-->
<!--          type="defult"-->
<!--          form-type="submit"-->
<!--          size="mini"-->
<!--          @getuserinfo="wxLogin"-->

<!--           style="width: 80%;background: #0BB20C;color: #fff;">授权登录</button>-->
   </div>
  </van-dialog>
</template>

<script>
    import {mapActions} from "vuex"
  export default{
    name: 'getUserinfo',
    props: ['empId'],
    data () {
      return {
        show: false,
        code:""
      }
    },

    methods: {
      init(){
        this.show=true;
      },
     wxLogin(){
       let that=this;
       console.log(123)
        wx.login({
           success(res) {
             console.log(res)
             that.code = res.code;
                wx.setStorageSync('openid',  that.code)
             that.setAppletCode(res.code)
             that.wxGetUserInfo(res.code)

           },
         })

     },
      ...mapActions(['setUser','setAppletCode','setIsAuthenticated']),
     wxGetUserInfo (code) {
       const that = this;
       wx.getUserInfo({
           withCredentials: true,
           success (res) {

             wx.setStorageSync('isGetUserInfo', 1)
             that.setUser(res)
             
           },
           fail (err) {
               console.log('自动wx.getUserInfo失败:',err);
               // 显示主动授权的button
               that.buttonVisible = true;
           }
       })
     },
      getUserInfo (event) {
        console.log(event.detail)
      },
      onClose () {
        this.show = false
      }
    }

  }
</script>

<style scoped>
  .body{
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding-top:30rpx;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
  }
</style>
